//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var dingyue = require("../../common/dingyue.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    ordernumber: '',
    buyallMa: '',
    buyallYoufei: '',
    shouhuorenAddress: '',
    shouhuoRen: '',
    shouhuoRenPhone: '',
    // 购买的商品列表
    buygoodsList: [],
    ordertime: '',
    // 默认收货地址
    defaultAddress: [],
    // 订单id
    orderid: '',
    //
    Ordertype: '0',
    contents: '',
    IsHaveSetExpress: '', //查看快递单号
    ExpressCompany: '',
    ExpressNumber: '',
    Type: 0,//是否自提
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.isbusinessOrder,`---"卖家isbusinessOrder"`);
    // // console.log(`这是${options.isbusinessOrder}卖家`);
    this.setData({
      uid: wx.getStorageSync('uid'),
      orderid: options.orderid,
      Ordertype: options.Ordertype,
      isbusinessOrder: options.isbusinessOrder,
    })
    // console.log(this.data.Ordertype);
    this.GetOrderInfo();
  },

  // 长按复制
  fuzhi: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        console.log(res);
        console.log("复制成功");
      }
    })
  },
  // 获取订单详情
  GetOrderInfo: function () {
    var that = this;
    var uid = this.data.uid;
    var orderid = this.data.orderid;
    var ordernumber = this.data.ordernumber;
    var buyallMa = this.data.buyallMa;
    var buyallYoufei = this.data.buyallYoufei;
    var businessname = this.data.businessname;
    var ordertime = this.data.ordertime;
    var buygoodsList = this.data.buygoodsList;
    var shouhuoRen = this.data.shouhuoRen;
    var shouhuoRenPhone = this.data.shouhuoRenPhone;
    var shouhuorenAddress = this.data.shouhuorenAddress;
    var IsHaveSetExpress = this.data.IsHaveSetExpress;
    var ExpressCompany = this.data.ExpressCompany;
    // var Type=that.data.Type;
    // console.log(that.data.Type);
    var ExpressNumber = this.data.ExpressNumber;
    console.log(baseJs.api_Order + "GetOrderInfo/" + orderid)
    wx.request({
      url: baseJs.api_Order + "GetOrderInfo/" + orderid,
      success: function (res) {
        console.log(res.data.ResultData)
        var json = res.data.ResultData;
        buyallMa = json.BuyAllMa;
        buyallYoufei = json.BuyAllYoufei;
        businessname = json.BusinessName;
        ordertime = json.OrderTime;
        ordernumber = json.OrderNumber;
        shouhuoRen = json.ShouHuoRen;
        shouhuoRenPhone = json.ShouHuoRenPhone;
        shouhuorenAddress = json.ShouHuoRenAddress;
        IsHaveSetExpress = json.IsHaveSetExpress;
        ExpressCompany = json.ExpressCompany;
        ExpressNumber = json.ExpressNumber;
        // that.data.Type=json.DeliverGoods
        // console.log(that.data.Type);
        var json1 = json.BuyGoodsList;
        for (var i = 0; i < json1.length; i++) {
          buygoodsList.push(json1[i])
        }

        that.setData({
          buyallMa: buyallMa,
          buyallYoufei: buyallYoufei,
          businessname: businessname,
          ordertime: ordertime,
          ordernumber: ordernumber,
          buygoodsList: buygoodsList,
          shouhuoRen: shouhuoRen,
          shouhuoRenPhone: shouhuoRenPhone,
          shouhuorenAddress: shouhuorenAddress,
          IsHaveSetExpress: IsHaveSetExpress,
          ExpressCompany: ExpressCompany,
          ExpressNumber: ExpressNumber,
          Type: json.DeliverGoods
        })
        // console.log(buygoodsList)
        console.log(that.data.Type);
        console.log(IsHaveSetExpress, "-----买家IsHaveSetExpress")
      },
      fail(res) {
        // console.log(res)
      }

    })
  },
  // 快递公司获取
  gongsiInput: function (e) {
    // console.log(e)
    var ExpressCompany = this.data.ExpressCompany;
    this.setData({
      ExpressCompany: e.detail.value,
    })
  },
  // 快递单号获取
  danhaoInput: function (e) {
    console.log(e)
    var ExpressNumber = this.data.ExpressNumber;
    this.setData({
      ExpressNumber: e.detail.value,
    })
  },
  // 扫一扫快递单号
  scanTap: function () {
    var that = this;
    var uid = this.data.uid;
    var ExpressNumber = this.data.ExpressNumber;
    // 扫一扫判断是否登录
    if (!uid) {
      wx.redirectTo({
        url: '/packageA/login/login',
      })
    }
    wx.scanCode({
      success(res) {
        // console.log(res);
        var ExpressNumber = res.result;
        that.setData({
          ExpressNumber: ExpressNumber
        })
      },

    })
  },

  // 提交快递单号
  submit: function (e) {
    var that = this;
    var uid = this.data.uid;
    var orderid = this.data.orderid;
    var ExpressCompany = this.data.ExpressCompany; //快递公司
    var ExpressNumber = this.data.ExpressNumber;  //快递单号
    // console.log(baseJs.api_Order + "SetOrderExpressInfo/" + '?' + "OrderId=" + orderid + "&UID=" + uid + "&ExpressCompany=" + ExpressCompany + "&ExpressNumber=" + ExpressNumber)
    wx.request({//提交快递单号
      url: baseJs.api_Order + "SetOrderExpressInfo/" + '?' + "OrderId=" + orderid + "&UID=" + uid + "&ExpressCompany=" + ExpressCompany + "&ExpressNumber=" + ExpressNumber,
      success(res) {
        var json = res.data;
        console.log(res);
        console.log(json)
        that.setData({
          ExpressCompany: ExpressCompany,
          ExpressNumber: ExpressNumber,
        })
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          // var openID = wx.getStorageSync('openID') //接收者的openID
          // var time = baseJs.getCurrentTimeStr().datetime//当前时间
          var BusinessAllName = wx.getStorageSync('BusinessAllName')//商家名称
          var shouhuoRenPhone = that.data.shouhuoRenPhone
          var shouhuorenAddress = that.data.shouhuorenAddress
          var addr = ''
          var reg = /.+?(省|市|自治区|自治州|县|区)/g // 省市区的正则
          for (let i = 0; i < shouhuorenAddress.match(reg).length; i++) {
            addr = addr + shouhuorenAddress.match(reg)[i]
          }
          console.log(addr);
          // console.log(ExpressNumber);
          // console.log(time);
          // 给买家微信发信息订单已经发货
          dingyue.fahuo(BusinessAllName, ExpressNumber, shouhuoRenPhone, addr)
        } else {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },


})